package com.cssy.xxkj.attach.dao.impl;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.byd.acegiext.domain.User;
import com.cssy.xxkj.attach.dao.AttachFileUploadBaseDirDAO;
import com.cssy.xxkj.attach.entity.AttachFileUploadBaseDirDO;
import com.cssy.xxkj.attach.util.CommConstants;
import com.plat.util.IDCodeTypeUtil;
import com.plat.util.SessionUtil;

public class AttachFileUploadBaseDirDAOImpl extends SqlMapClientDaoSupport implements AttachFileUploadBaseDirDAO {

	@Override
	public AttachFileUploadBaseDirDO createAttachBaseDir(HttpServletRequest request) {
		
		List<AttachFileUploadBaseDirDO> baseDirDOs = this.findFileUploadBaseDirs(CommConstants.UPLOAD_BASE_DIR);
		
		if(baseDirDOs != null && baseDirDOs.size() == 1){
			return baseDirDOs.get(0);
		}
		
		if(baseDirDOs !=null && baseDirDOs.size() > 0){
			throw new RuntimeException("取得AttachFileUploadBaseDir异常！");
		}
		
		AttachFileUploadBaseDirDO fileUploadBaseDir = new AttachFileUploadBaseDirDO();
		fileUploadBaseDir.setId(IDCodeTypeUtil.getId());
		fileUploadBaseDir.setDirName(CommConstants.UPLOAD_BASE_DIR);
		fileUploadBaseDir.setDirPath(CommConstants.UPLOAD_BASE_DIR);
		User user = SessionUtil.getUser(request);
		fileUploadBaseDir.setDirUser(user.getId()+"");
		fileUploadBaseDir.setDirUserName(user.getName());
		fileUploadBaseDir.setDirDate(new Date());
		this.save(fileUploadBaseDir);
		return fileUploadBaseDir;
	}

	
	private List<AttachFileUploadBaseDirDO> findFileUploadBaseDirs(String dirPath){
		AttachFileUploadBaseDirDO uploadBaseDir = new AttachFileUploadBaseDirDO();
		uploadBaseDir.setDirPath(dirPath);
		return getSqlMapClientTemplate().queryForList("AttachFileUploadBaseDir.findListByDO", uploadBaseDir);
	}
	
	public void save(AttachFileUploadBaseDirDO fileUploadBaseDir){
		getSqlMapClientTemplate().insert("AttachFileUploadBaseDir.insert", fileUploadBaseDir);
	}
	
}
